clear all;
clc;

% takes a file for an MRG axon
% - contains potentials for N-M-F-S-S-S-F-M-N
% resaves a file for a Sweeney axon
% - extracts only N (1, 9, 17, etc...)

patnum=5;
ptag=['p',num2str(patnum),'_'];

dval = [3,6,9,12,15];
nodiam = length(dval);

epidur = 0;
if(epidur==1)
    predc = 'epi_bp_dc_phi';
    predr = 'epi_bp_dr_phi';
else
    predc = 'sub_bp_dc_phi';
    predr = 'sub_bp_dr_phi';    
end

length_dc = 280;
length_dr = 150;

% -------------------- Importing Data -------------------------------------

dcdata = cell(nodiam,1);
drdata = cell(nodiam,1);

cd(['p',num2str(patnum),'_mrgpotentials\']);
for ii = 1:nodiam
    
    D = dval(ii);
    
    dcdata{ii} = load([ptag,predc,'_',num2str(D),'um.txt']);
    drdata{ii} = load([ptag,predr,'_',num2str(D),'um.txt']);
    
end
cd ..;

% -------------------- Processing Data ------------------------------------

dc_newdata = cell(nodiam,1);
dr_newdata = cell(nodiam,1);

for ii = 1:nodiam
    dc_newdata{ii} = dcdata{ii}(:,1:8:end);
    dr_newdata{ii} = drdata{ii}(:,1:8:end);
end

return

% -------------------- Saving Data ----------------------------------------

cd(['p',num2str(patnum),'_swpotentials\']);

for ii = 1:nodiam
    
    D = dval(ii);
    dlmwrite([ptag,predc,'_',num2str(D),'um.txt'],dc_newdata{ii},...
             'precision',8,'delimiter',' ');
    dlmwrite([ptag,predr,'_',num2str(D),'um.txt'],dr_newdata{ii},...
             'precision',8,'delimiter',' ');
end

cd ..;
